#include <stdio.h>
#include <string.h>

int dict[7][27];

void prebuild_dict() //dict[0,len][a,z]
{
    for(int i = 0;i < 27;++i)
    {
        dict[0][i] = 0;
        dict[1][i] = 1;
    }
    for(int i = 2;i < 7;++i)
    {
        for(int j = 26;j >= 1;--j)
        {
            if(j == 26) dict[i][j] = 0;
            else
            {
                dict[i][j] = dict[i][j+1] + dict[i-1][j+1];
            }
        }
    }
    for(int i = 0;i < 7;++i)
    {
        dict[i][0] = 0;
        for(int j = 1;j < 27;++j)
        {
            dict[i][0] += dict[i][j];
            printf("%d ",dict[i][j]);
        }
        printf("\nsum of len %d: %d\n",i,dict[i][0]);
    }
}

int f(char* a)
{
    int len = strlen(a);
    printf("len: %d\n",len);
    int ret = 0;
    for(int i = 1;i <= len-1;++i)
    {
        ret += dict[i][0];
    }
    printf("%d\n",ret);

    int lb = 1,ub;
    for(int i = 0;i < len;++i)
    {
        ub = a[i]-'a'+1;
        if(i == len-1) ++ub;
        for(int j = lb;j < ub;++j)
        {
            ret += dict[len-i][j];
        }
        if(lb == ub) ++ub;
        printf("%c (%d,%d) %d\n",a[i],lb,ub,ret);
        lb = ub;
    }
    return ret;
}

int main()
{
    prebuild_dict();
    int n;
    scanf("%d",&n);
    char a[10];
    for(int i = 0;i < n;++i)
    {
        scanf("%s",a);
        printf("%d\n",f(a));
    }
    return 0;
} 